package com.wanmait.recycle.redis.dao.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.wanmait.recycle.redis.dao.RedisPraiseArticleDao;

@Repository
public class RedisPraiseArticleDaoImpl implements RedisPraiseArticleDao {

	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	
	//key为：praiseArticle:1:userinfoId:set
	
	/*添加点赞*/
	@Override
	public void addPraiseArticle(Integer articleId, Integer userinfoId) {
		redisTemplate.opsForSet().add("praiseArticle:"+articleId+":userinfoId:set", userinfoId.toString());

	}
	
	/*是否点赞*/
	@Override
	public boolean isPraise(Integer articleId, Integer userinfoId) {
		
		return redisTemplate.opsForSet().isMember("praiseArticle:"+articleId+":userinfoId:set", userinfoId.toString());
	}

	/*取消点赞*/
	@Override
	public void removePraiseArticle(Integer articleId, Integer userinfoId) {
		redisTemplate.opsForSet().remove("praiseArticle:"+articleId+":userinfoId:set", userinfoId.toString());

	}

	//类型为String，问题的点赞数量 praiseArticle:id:count
	/*设置问题点赞数量*/
	@Override
	public void setCount(Integer articleId, Integer praiseCount) {
		redisTemplate.opsForValue().set("praiseArticle:"+articleId+":praiseCount",praiseCount.toString());;

	}

	/*点赞数加*/
	@Override
	public void increaseCount(Integer articleId) {
		redisTemplate.opsForValue().increment("praiseArticle:"+articleId+":praiseCount", 1);

	}

	/*点赞数减*/
	@Override
	public void decreaseCount(Integer articleId) {
		redisTemplate.opsForValue().increment("praiseArticle:"+articleId+":praiseCount", -1);

	}

	//获得点赞数量
	@Override
	public int getCount(Integer articleId) {
			
		try {
			
			String praiseCount = (String)redisTemplate.opsForValue().get("praiseArticle:"+articleId+":praiseCount");
			return Integer.parseInt(praiseCount);
		} 
		catch (NumberFormatException e) {
			return 0;
		}
		
	}

	@Override
	public void addInit(Integer articleId) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean hasInit(Integer articleId) {
		// TODO Auto-generated method stub
		return false;
	}

	//userLike:list {“articleId”:1,”userinfoId”:1,delete:1,inputTime:毫秒数}
	//delete为1表示取消点赞，为0表示点赞
	@Override
	public void leftPush(String message) {
		redisTemplate.opsForList().leftPush("praiseArticle:list", message);

	}

	//取出消息
	@Override
	public String rightPop() {
		
		return (String)redisTemplate.opsForList().rightPop("praiseArticle:list");
	}

	//最近操作了哪些文章,类型为set，key为userLike:update:set 存放文章id
	@Override
	public void addUpdate(Integer articleId) {
		redisTemplate.opsForSet().add("praiseArticle:update:set", articleId.toString());

	}

	//获得上次定时器同步点赞数量后被操作（点赞 取消点赞）的所有的文章id
	/*迭代器(Iterator)是一个对象，它的工作是遍历并选择序列中的对象，
	它提供了一种访问一个容器(container)对象中的各个元素，而又不必暴露该对象内部细节的方法。
	通过迭代器，开发人员不需要了解容器底层的结构，就可以实现对容器的遍历*/
	@Override
	public Set<Integer> getUpdate() {
		//定义类型为Integer的set集合
		Set<Integer> setInts = new HashSet<>();
		//取出redis中的元素
		Set<Object> setObjs = redisTemplate.opsForSet().members("praiseArticle:update:set");
		//迭代器(Iterator)是一个对象，它的工作是遍历并选择序列中的对象
		Iterator<Object> it = setObjs.iterator();
		while(it.hasNext())
		{
			setInts.add(Integer.parseInt((String)it.next()));
		}
		return setInts;
	}

	/*同步后清空键"praiseArticle:update:set"中的值*/
	@Override
	public void removeUpdate() {
		redisTemplate.opsForSet().remove("praiseArticle:update:set");
		
	}

}
